 /** All rights Reserved, Copyright (C) Aisino LIMITED 2019
 * FileName: MyRunnable.java
 * Version:  1.0
 * Modify record:
 * NO. |     Date       |    Name        |      Content
 * 1   | 2019年4月1日        | Aisino)Jack    | original version
 */
package com.jack.hhitseat.task;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.alibaba.fastjson.JSON;
import com.jack.hhitseat.bean.AppBean;
import com.jack.hhitseat.bean.Hhit;
import com.jack.hhitseat.bean.User;
import com.jack.hhitseat.service.HttpClient;
import com.jack.hhitseat.service.impl.HhitService;
import com.jack.hhitseat.service.impl.LogServiceImpl;
import com.jack.hhitseat.utils.MyUtils;

/**
 * class name: MyRunnable <BR>
 * class description: please write your description <BR>
 * @version 1.0  2019年4月1日 上午8:57:03
 * @author Aisino)Jack
 */
//为了线程安全，不能依赖注入！！！
public class InfoRunnable extends Thread {
	private final Logger logger = LoggerFactory.getLogger(InfoRunnable.class);
	private final static String KEY = "16";
	private static String url = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/login.aspx"; 
	private int num;
	private static HttpClient httpClient = new HttpClient();
	private static HhitService hhitService = (HhitService) AppBean.getBean("hhitService");

	public InfoRunnable() {
	}
	
	public InfoRunnable(int num) {
		this.num = num;
	}
	
	@Override
	public synchronized void run() {
		final int COUNT = 600;
		  MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
		  String sjc = (System.currentTimeMillis()+10000)+"";
		  
		  params.add("act","dlogin");
		  params.add("number", KEY);
		  params.add("id", num+""); 
		  params.add("pwd", num+""); 
		  params.add("role", "512");
		  params.add("_nocache", sjc);
		  
		ResponseEntity<String> response =  HttpClient.login2wx(url, null, params);
		String session = "";
		
		try {
			session = response.getHeaders().toString().split("ASP.NET_SessionId=")[1].split("; path=/; ")[0];
		} catch (Exception e) {
			ResponseEntity<String> response2 =  HttpClient.login2wx(url, null, params);
			session = response2.getHeaders().toString().split("ASP.NET_SessionId=")[1].split("; path=/; ")[0];
		}

		for (int i = 1; i <= COUNT; i++) {
			String body =  response.getBody();
//			System.out.println(body);
			if(!body.contains("验证码")) {
				logger.warn(body);
				if(body.contains("密码")) {
					logger.warn("密码错误===={}", num);
					return;
				}else if(body.contains("未授权")){
					logger.error("未授权用户==={}", num);
					return;
				}
				
				try {
					if(hhitService.count(num) >=1) {
						return;
					}
					body = body.split("\"data\":")[1].split(",\"ext\":null}")[0];
					
					Hhit info = new Hhit();
					info = JSON.parseObject(body, Hhit.class);
					info.setSno(info.getId());
					info.setId(null);
					info.setPwd(num+"");
					info.setCreateTime(MyUtils.getNowDateTime());
					hhitService.save(info);
				}catch (Exception e) {
					logger.error("num===={}==={}===", e, body);
				}
				return;
			} else {
				httpClient.yzm(session);
				response =  HttpClient.login2wx(url, session, params);
			}
		}
	}
}
